package com.bwie.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.domain.TbLog;
import com.bwie.domain.TbProd;
import com.bwie.mapper.TbProdMapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;

import java.util.Date;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: 库存预警定时任务
 * @date 2024/11/21 15:48
 */

@Slf4j
public class StockWaringJob implements Job {

    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    KafkaTemplate kafkaTemplate;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("库存预警定时任务执行了");
        List<TbProd> tbProds = tbProdMapper.selectList(
                new QueryWrapper<TbProd>().lambda().lt(TbProd::getProdStock, 10)
        );
        for (TbProd tbProd : tbProds) {
            log.info("库存预警：{}{}", tbProd.getProdName(), tbProd.getProdStock());

            TbLog tbLog = new TbLog();
            tbLog.setLogTitle("库存预警");
            tbLog.setLogContent("商品信息：" + JSON.toJSONString(tbProd));
            tbLog.setCreateTime(new Date());

            kafkaTemplate.send("topic-prod", JSON.toJSONString(tbLog));
        }
    }
}
